package cn.edu.ldu.util;

import cn.edu.ldu.protocol.Protocol;
import cn.edu.ldu.views.login.LoginUI;
import cn.edu.ldu.views.main.MainUI;

import javax.swing.*;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class ReceiveMessage extends Thread {
    private DatagramSocket clientSocket;            //会话套件字
    private MainUI parentUI;                        //父类
    private byte[] data = new byte[8096];

    //构造函数
    public ReceiveMessage(DatagramSocket socket,MainUI mainUI){
        clientSocket = socket;
        this.parentUI=mainUI;
    }
    @Override
    public void run(){
        while (true){
            try{
                DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName(Consts.ipv4), Consts.port);

                try {
                    clientSocket.receive(packet);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                L.OK(packet, ReceiveMessage.class);

                Protocol pro = (Protocol)Translate.ByteToObject(packet.getData());
                pro.key();

            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
